package moni;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Rediswdc {
    // 使用 HashMap 来存储键值对
    ConcurrentHashMap map = new ConcurrentHashMap();
    private Map<String, String> storage = new HashMap<>();

    // 设置键值对
    public void set(String key, String value) {
        storage.put(key, value);
        System.out.println("SET " + key + " " + value);
    }

    // 获取键对应的值
    public String get(String key) {
        String value = storage.get(key);
        if (value != null) {
            System.out.println("GET " + key + " " + value);
        } else {
            System.out.println("GET " + key + " (nil)");
        }
        return value;
    }

    // 删除键值对
    public void del(String key) {
        String value = storage.remove(key);
        if (value != null) {
            System.out.println("DEL " + key + " " + value);
        } else {
            System.out.println("DEL " + key + " (nonexistent)");
        }
    }

    // 检查键是否存在
    public boolean exists(String key) {
        boolean exists = storage.containsKey(key);
        System.out.println("EXISTS " + key + " " + (exists ? "1" : "0"));
        return exists;
    }

    // 展示所有键值对
    public void keys() {
        System.out.println("KEYS:");
        for (String key : storage.keySet()) {
            System.out.println(key);
        }
    }

    public static void main(String[] args) {
        Rediswdc redis = new Rediswdc();
        redis.set("name", "Alice");
        redis.get("name");
        redis.set("age", "30");
        redis.get("age");
        redis.del("name");
        redis.get("name");
        redis.exists("age");
        redis.keys();
    }
}
